/*** Eclipse Class Decompiler plugin, copyright (c) 2012 Chao Chen (cnfree2000@hotmail.com) ***/
package com.feiyue;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.*;
import android.os.*;
import android.view.*;
import android.widget.*;

import com.feiyue.basic.reader.PageActivity;
import com.feiyue.basic.server.cache.CacheFetch;
import com.feiyue.basic.server.cache.DiskLruCache;
import com.feiyue.basic.server.db.BookCollectDBHandler;
import com.feiyue.basic.server.dl.DlService;
import com.feiyue.basic.server.net.NovelInfo;
import com.feiyue.basic.server.parser.Book;
import com.feiyue.basic.server.parser.Comment;
import com.feiyue.basic.server.parser.OneComment;
import com.feiyue.helper.Thread2SendFavoriteReport;
import com.umeng.analytics.MobclickAgent;

import java.io.File;
import java.util.List;

// Referenced classes of package com.feiyue:
//          BookCommentActivity, BookCatologActivity

public class BookBriefActivity extends Activity
    implements android.view.View.OnTouchListener, android.view.GestureDetector.OnGestureListener
{
    private class GetDataTask extends AsyncTask
    {

        private Comment tempComment;

        protected Object doInBackground(Object aobj[])
        {
            return doInBackground((Void[])aobj);
        }

        protected String[] doInBackground(Void avoid[])
        {
            tempComment = NovelInfo.getComments(context, NovelInfo.getUrl2Comment(bookid, 0, 3), bookid, 0, 3);
            return null;
        }

        protected void onPostExecute(Object obj)
        {
            onPostExecute((String[])obj);
        }

        protected void onPostExecute(String as[])
        {
            if (tempComment == null || tempComment.getList() == null || tempComment.getList().size() == 0)
            {
                morecomment.setText("        我要评论        ");
                novel_brief_comment_title.setText("小说评论(共0条)");
            } else
            {
                int i = tempComment.getList().size();
                Object aobj[] = new Object[1];
                aobj[0] = Integer.valueOf(tempComment.getCount());
                String s = String.format("小说评论(共%d条)", aobj);
                novel_brief_comment_title.setText(s);
                int j = 0;
                while (j < 3 && j < i) 
                {
                    LinearLayout linearlayout = (LinearLayout)((LayoutInflater)getSystemService("layout_inflater")).inflate(0x7f030014, null);
                    TextView textview = (TextView)linearlayout.findViewById(0x7f0600b3);
                    TextView textview1 = (TextView)linearlayout.findViewById(0x7f0600b4);
                    TextView textview2 = (TextView)linearlayout.findViewById(0x7f0600b5);
                    textview.setText(((OneComment)tempComment.getList().get(j)).getIp());
                    textview1.setText(((OneComment)tempComment.getList().get(j)).getTime());
                    textview2.setText(((OneComment)tempComment.getList().get(j)).getInfo());
                    android.view.ViewGroup.LayoutParams layoutparams = new android.view.ViewGroup.LayoutParams(-1, -2);
                    partialcomments.addView(linearlayout, layoutparams);
                    j++;
                }
            }
            super.onPostExecute(as);
        }
    }

    private class MyThread2image extends Thread
    {

        private Context context;
        private String coverAddress;
        private Handler handler;
        private int height;
        private Myrunnable2image myrunnable;
        private int width;

        public void run()
        {
            File file = CacheFetch.getFile(context, coverAddress, coverAddress, false);
            if (file != null) {
            	try {
                    Bitmap bitmap = BitmapFactory.decodeFile(file.getPath());
                    
                    if (bitmap != null)
                    {
                        int i = bitmap.getWidth();
                        int j = bitmap.getHeight();
                        float f = (float)((double)width / (double)i);
                        float f1 = (float)((double)height / (double)j);
                        Matrix matrix = new Matrix();
                        matrix.postScale(f, f1);
                        Bitmap bitmap1 = Bitmap.createBitmap(bitmap, 0, 0, i, j, matrix, true);
                        myrunnable.setBitmap(bitmap1);
                        handler.post(myrunnable);
                    }
            	} catch (OutOfMemoryError e) {
            		System.gc();
            	}
            	
            }
        }

        public MyThread2image(Context context1, Myrunnable2image myrunnable2image, Handler handler1, String s, int i, int j)
        {
            super();
            context = context1;
            myrunnable = myrunnable2image;
            handler = handler1;
            coverAddress = s;
            width = i;
            height = j;
        }
    }

    private class Myrunnable2image
        implements Runnable
    {

        private Bitmap bitmap;
        private ImageView coverImageView;

        public void run()
        {
            coverImageView.setImageBitmap(bitmap);
        }

        public void setBitmap(Bitmap bitmap1)
        {
            bitmap = bitmap1;
        }

        public Myrunnable2image(ImageView imageview)
        {
            super();
            coverImageView = null;
            bitmap = null;
            coverImageView = imageview;
        }
    }


    private String author;
    private Book book;
    private int bookid;
    private String bookname;
    private int categoryid;
    private String categoryname;
    private Context context;
    private String coverAddr;
    private int coverid;
    private int dlstate;
    private double height;
    private String introduction;
    private String issuestate;
    private GestureDetector mGestureDetector;
    private int minVelocity;
    private Button morecomment;
    private TextView novel_brief_comment_title;
    private LinearLayout partialcomments;
    private ImageView profile_header_back;
    private Button textViewfavorite;
    private int verticalMinDistance;
    private double width;

    public BookBriefActivity()
    {
        book = new Book();
        verticalMinDistance = 200;
        minVelocity = 150;
    }

    private void fillImage(ImageView imageview, String s)
    {
        int i = BitmapFactory.decodeResource(getResources(), 0x7f020004).getWidth();
        int j = BitmapFactory.decodeResource(getResources(), 0x7f020004).getHeight();
        Myrunnable2image myrunnable2image = new Myrunnable2image(imageview);
        Handler handler = new Handler();
        (new MyThread2image(getApplicationContext(), myrunnable2image, handler, s, i, j)).start();
    }

    private android.view.View.OnClickListener getBookaddOnclick()
    {
        return new android.view.View.OnClickListener() {

            public void onClick(View view)
            {
                BookCollectDBHandler bookcollectdbhandler;
				try {
					bookcollectdbhandler = new BookCollectDBHandler(context);
					bookcollectdbhandler.insert(book, context);
	                bookcollectdbhandler.destroy();
				} catch (Exception e) {
					e.printStackTrace();
				}
                
                Context context1 = context;
                Object aobj[] = new Object[1];
                aobj[0] = bookname;
                Toast.makeText(context1, String.format("已收藏 %s", aobj), 0).show();
                textViewfavorite.setText("取消收藏");
                textViewfavorite.setOnClickListener(getBookdeleteOnclick());
                Context context2 = context;
                Object aobj1[] = new Object[1];
                aobj1[0] = Integer.valueOf(book.getId());
                (new Thread2SendFavoriteReport(context2, String.format("http://book.yy567.net/bookCollection?type=collect&bookid=%s", aobj1))).start();
            }
        };
    }

    private android.view.View.OnClickListener getBookcatologOnclick()
    {
        return new android.view.View.OnClickListener() {

            public void onClick(View view)
            {
                Intent intent = new Intent(getApplicationContext(), BookCatologActivity.class);
                intent.putExtra("categoryname", categoryname);
                intent.putExtra("categoryid", categoryid);
                intent.putExtra("bookname", bookname);
                intent.putExtra("bookid", bookid);
                intent.putExtra("author", author);
                intent.putExtra("introduction", introduction);
                intent.putExtra("issuestate", issuestate);
                startActivity(intent);
            }
        };
    }

    private android.view.View.OnClickListener getBookdeleteOnclick()
    {
        return new android.view.View.OnClickListener() {

            public void onClick(View view)
            {
                BookCollectDBHandler bookcollectdbhandler;
				try {
					bookcollectdbhandler = new BookCollectDBHandler(context);
					bookcollectdbhandler.delete(context, book);
	                bookcollectdbhandler.destroy();
				} catch (Exception e) {
					e.printStackTrace();
				}
                
                Context context1 = context;
                Object aobj[] = new Object[1];
                aobj[0] = bookname;
                Toast.makeText(context1, String.format("已取消收藏 %s", aobj), 0).show();
                textViewfavorite.setText("收藏");
                textViewfavorite.setOnClickListener(getBookaddOnclick());
            }
        };
    }

    private android.view.View.OnClickListener getBookreadOnclick()
    {
        return new android.view.View.OnClickListener() {

            public void onClick(View view)
            {
                Intent intent = new Intent(context, PageActivity.class);
                intent.putExtra("categoryname", categoryname);
                intent.putExtra("categoryid", categoryid);
                intent.putExtra("bookname", bookname);
                intent.putExtra("bookid", bookid);
                intent.putExtra("author", author);
                intent.putExtra("introduction", introduction);
                intent.putExtra("issuestate", issuestate);
                intent.putExtra("chapterid", -1);
                intent.putExtra("chapterNum", -1);
                startActivity(intent);
            }
        };
    }

    public boolean dispatchTouchEvent(MotionEvent motionevent)
    {
        boolean flag;
        if (!mGestureDetector.onTouchEvent(motionevent))
            flag = super.dispatchTouchEvent(motionevent);
        else
            flag = true;
        return flag;
    }

    public void onCreate(Bundle bundle)
    {
        super.onCreate(bundle);
        setContentView(R.layout.bookbrief);
        partialcomments = (LinearLayout)findViewById(R.id.partialcomments);
        novel_brief_comment_title = (TextView)findViewById(R.id.novel_brief_comment_title);
        morecomment = (Button)findViewById(R.id.morerecomment);
        morecomment.setOnClickListener(new android.view.View.OnClickListener() {

            public void onClick(View view)
            {
                Intent intent = new Intent(context, BookCommentActivity.class);
                intent.putExtra("bookid", bookid);
                startActivity(intent);
            }
        });
        mGestureDetector = new GestureDetector(this);
        LinearLayout linearlayout = (LinearLayout)findViewById(R.id.cover_view);
        linearlayout.setOnTouchListener(this);
        linearlayout.setLongClickable(true);
        context = getApplicationContext();
        width = BitmapFactory.decodeResource(context.getResources(), 0x7f020004).getWidth();
        height = BitmapFactory.decodeResource(context.getResources(), 0x7f020004).getHeight();
        categoryname = getIntent().getExtras().getString("categoryname");
        categoryid = getIntent().getExtras().getInt("categoryid");
        book.setCategoryId(categoryid);
        bookname = getIntent().getExtras().getString("bookname");
        book.setName(bookname);
        bookid = getIntent().getExtras().getInt("bookid");
        book.setId(bookid);
        coverAddr = getIntent().getExtras().getString("coverAddr");
        book.setCoverAddr(coverAddr);
        author = getIntent().getExtras().getString("author");
        book.setAuthor(author);
        introduction = getIntent().getExtras().getString("introduction");
        book.setIntroduction(introduction);
        issuestate = getIntent().getExtras().getString("issuestate");
        book.setIssuestate(issuestate);
        coverid = getIntent().getExtras().getInt("coverid");
        book.setCoverid(coverid);
        dlstate = getIntent().getExtras().getInt("dlstate");
        book.setdlstate(dlstate);
        (new GetDataTask()).execute(new Void[0]);
        ((TextView)findViewById(0x7f060016)).setText(bookname);
        ((TextView)findViewById(0x7f060018)).setText(author);
        ((TextView)findViewById(0x7f060020)).setText(introduction);
        ((TextView)findViewById(0x7f060019)).setText(issuestate);
        ((Button)findViewById(R.id.bookcatolog)).setOnClickListener(getBookcatologOnclick());
        textViewfavorite = (Button)findViewById(0x7f06001b);
        BookCollectDBHandler bookcollectdbhandler;
		try {
			bookcollectdbhandler = new BookCollectDBHandler(context);
			if (!bookcollectdbhandler.isExist(book)) {
	        	textViewfavorite.setText("收藏");
	            textViewfavorite.setOnClickListener(getBookaddOnclick());
	        } else {
	        	textViewfavorite.setText("取消收藏");
	            textViewfavorite.setOnClickListener(getBookdeleteOnclick());
	        }
	        
	        bookcollectdbhandler.destroy();
		} catch (Exception e) {
			e.printStackTrace();
		}
        
        ImageView imageview;
        DiskLruCache disklrucache;
        ((Button)findViewById(R.id.readbtn)).setOnClickListener(getBookreadOnclick());
        imageview = (ImageView)findViewById(0x7f060015);
        if (book.getCoverAddr() == null || book.getCoverAddr().equals("null") || book.getCoverAddr().equals("")) {
        	imageview.setImageResource(book.getCoverid(Boolean.valueOf(false)));
        } else {
        	File file = DiskLruCache.getDiskCacheDir(context, "http");
            disklrucache = DiskLruCache.openCache(context, file, 0xa00000L);
            if (disklrucache == null) {
            	imageview.setImageResource(book.getCoverid(Boolean.valueOf(false)));
            } else {
            	File file1 = new File(disklrucache.createFilePath(book.getCoverAddr()));
                if (!disklrucache.containsKey(book.getCoverAddr())) {
                	imageview.setImageResource(book.getCoverid(Boolean.valueOf(false)));
                    fillImage(imageview, book.getCoverAddr());
                } else {
                	Bitmap bitmap = null;
                    Bitmap bitmap1 = BitmapFactory.decodeFile(file1.getPath());
                    bitmap = bitmap1;
                    
                    if (bitmap != null)
                    {
                        int i = bitmap.getWidth();
                        int j = bitmap.getHeight();
                        float f = (float)(width / (double)i);
                        float f1 = (float)(height / (double)j);
                        Matrix matrix = new Matrix();
                        matrix.postScale(f, f1);
                        imageview.setImageBitmap(Bitmap.createBitmap(bitmap, 0, 0, i, j, matrix, true));
                    }
                }
            }
        }
        
        ImageView imageview1 = (ImageView)findViewById(0x7f06001a);
        if (book.getdlstate() == 0)
        {
            imageview1.setVisibility(0);
            imageview1.setOnClickListener(new android.view.View.OnClickListener() {

                public void onClick(View view)
                {
                    Intent intent;
                    try
                    {
                        BookCollectDBHandler bookcollectdbhandler1 = new BookCollectDBHandler(context);
                        if (!bookcollectdbhandler1.isExist(book))
                        {
                            bookcollectdbhandler1.insert(book, context);
                            Context context1 = context;
                            Object aobj[] = new Object[1];
                            aobj[0] = bookname;
                            Toast.makeText(context1, String.format("已收藏 %s", aobj), 0).show();
                            Context context2 = context;
                            Object aobj1[] = new Object[1];
                            aobj1[0] = Integer.valueOf(book.getId());
                            (new Thread2SendFavoriteReport(context2, String.format("http://book.yy567.net/bookCollection?type=collect&bookid=%s", aobj1))).start();
                        }
                        bookcollectdbhandler1.destroy();
                    }
                    catch (Exception exception1) { }
                    intent = new Intent(context, DlService.class);
                    intent.putExtra("categoryid", book.getCategoryId());
                    intent.putExtra("bookid", book.getId());
                    intent.putExtra("bookname", book.getName());
                    intent.putExtra("issuestate", book.getIssuestate());
                    context.startService(intent);
                }
            });
        } else
        {
            imageview1.setImageResource(0x7f020027);
            imageview1.setVisibility(0);
        }
        profile_header_back = (ImageView)findViewById(0x7f06000d);
        profile_header_back.setOnClickListener(new android.view.View.OnClickListener() {

            public void onClick(View view)
            {
                finish();
                overridePendingTransition(0x7f04000a, 0x7f04000d);
            }
        });
    }

    protected void onDestroy()
    {
        super.onDestroy();
    }

    public boolean onDown(MotionEvent motionevent)
    {
        return false;
    }

    public boolean onFling(MotionEvent motionevent, MotionEvent motionevent1, float f, float f1)
    {
        boolean flag = true;
        float f2 = Math.abs(motionevent.getX() - motionevent1.getX()) / Math.abs(motionevent.getY() - motionevent1.getY());
        if (motionevent.getX() - motionevent1.getX() > 0.0F && (double)f2 - 2.5D > 0.0D)
        {
            finish();
            overridePendingTransition(0x7f04000b, 0x7f04000c);
        } else
        if (motionevent.getX() - motionevent1.getX() < 0.0F && (double)f2 - 2.5D > 0.0D)
        {
            finish();
            overridePendingTransition(0x7f04000a, 0x7f04000d);
        } else
        {
            flag = false;
        }
        return flag;
    }

    public boolean onKeyDown(int i, KeyEvent keyevent)
    {
        boolean flag;
        if (i == 4)
        {
            finish();
            overridePendingTransition(0x7f04000a, 0x7f04000d);
            flag = true;
        } else
        {
            flag = super.onKeyDown(i, keyevent);
        }
        return flag;
    }

    public void onLongPress(MotionEvent motionevent)
    {
    }

    protected void onPause()
    {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    protected void onResume()
    {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    public boolean onScroll(MotionEvent motionevent, MotionEvent motionevent1, float f, float f1)
    {
        return false;
    }

    public void onShowPress(MotionEvent motionevent)
    {
    }

    public boolean onSingleTapUp(MotionEvent motionevent)
    {
        return false;
    }

    public boolean onTouch(View view, MotionEvent motionevent)
    {
        return mGestureDetector.onTouchEvent(motionevent);
    }
}